package com.haitaoit.chengquan.module.center.adapter.yuyue;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center.model.MyYuyueModel;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/12.
 */
public class MyYuyueAdapter extends BaseAdapter {
    private Context            context;
    private BackCall           backCall;
    private List<MyYuyueModel> list;
    private String             title_type;

    public void setTitle_type(String title_type) {
        this.title_type = title_type;
    }

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public MyYuyueAdapter(Context context) {
        this.context = context;
    }

    public List<MyYuyueModel> getList() {
        return list;
    }

    public void setList(List<MyYuyueModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.my_yuyue_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.yuyue_itemll:
                    case R.id.item_ll:
                        MyYuyueAdapter.this.backCall.deal(R.id.yuyue_itemll, position);
                        break;
                    case R.id.cancel_yuyue:
                        MyYuyueAdapter.this.backCall.deal(R.id.cancel_yuyue, position);
                        break;
                    case R.id.go_pay:
                        MyYuyueAdapter.this.backCall.deal(R.id.go_pay, position);
                        break;
                    case R.id.confirm_complete:
                        MyYuyueAdapter.this.backCall.deal(R.id.confirm_complete, position);
                        break;
                    case R.id.go_eva:
                        MyYuyueAdapter.this.backCall.deal(R.id.go_eva, position);
                        break;
                    case R.id.detele_yuyue:
                        MyYuyueAdapter.this.backCall.deal(R.id.detele_yuyue, position);
                        break;
                    case R.id.chenge_yuyue:
                        MyYuyueAdapter.this.backCall.deal(R.id.chenge_yuyue, position);
                        break;
                }
            }
        };
        holder.yuyue_itemll.setOnClickListener(listener);
        holder.item_ll.setOnClickListener(listener);
        holder.cancel_yuyue.setOnClickListener(listener);
        holder.go_pay.setOnClickListener(listener);
        holder.confirm_complete.setOnClickListener(listener);
        holder.go_eva.setOnClickListener(listener);
        holder.detele_yuyue.setOnClickListener(listener);

        holder.cancel_yuyue.setVisibility(View.GONE);// 取消订单
        holder.go_pay.setVisibility(View.GONE);// 付款
        holder.confirm_complete.setVisibility(View.GONE);// 确认完成
        holder.go_eva.setVisibility(View.GONE);// 去评价
        holder.detele_yuyue.setVisibility(View.GONE);// 删除订单
        holder.chenge_yuyue.setVisibility(View.GONE);// 修改订单
        holder.yuyue_buttonll.setVisibility(View.VISIBLE);
        /**
         * status:订单状态1未支付(显示取消订单和付款)
         * 2已支付(is_confirm:1显示完成按钮2不显示)
         * 3已完成(is_evaluation为1显示区评价)4已评价5已取消(显示删除订单)
         *
         * status:订单状态1已下单(显示取消订单)
         * 2已确认(显示取消订单和去付款 is_update_order:1不显示付完款2显示付款)
         * 3已支付(is_confirm:1显示完成按钮2不显示)
         * 4已完成(is_evaluation为1显示区评价)5已评价6已取消(显示删除订单)
         * */
        if (title_type.equals("1")) {
            if (list.get(position).getStatus().equals("1")) {
                holder.yuyue_state.setText("未支付");
                holder.cancel_yuyue.setVisibility(View.VISIBLE);
                holder.go_pay.setVisibility(View.VISIBLE);
            } else if (list.get(position).getStatus().equals("2")) {
                holder.yuyue_state.setText("已支付");
                if (list.get(position).getIs_confirm().equals("1")) {
                    holder.confirm_complete.setVisibility(View.VISIBLE);
                } else {
                    holder.yuyue_buttonll.setVisibility(View.GONE);
                }
            } else if (list.get(position).getStatus().equals("3")) {
                holder.yuyue_state.setText("已完成");
                if (list.get(position).getIs_evaluation().equals("1")) {
                    holder.go_eva.setVisibility(View.VISIBLE);// 去评价
                } else {
                    holder.yuyue_buttonll.setVisibility(View.GONE);
                }
            } else if (list.get(position).getStatus().equals("4")) {
                holder.yuyue_state.setText("已评价");
                holder.yuyue_buttonll.setVisibility(View.VISIBLE);
                holder.detele_yuyue.setVisibility(View.VISIBLE);
            } else if (list.get(position).getStatus().equals("5")) {
                holder.yuyue_state.setText("已取消");
                holder.detele_yuyue.setVisibility(View.VISIBLE);
            }
        } else if (title_type.equals("2")) {

            if (list.get(position).getStatus().equals("1")) {
                holder.yuyue_state.setText("已下单");
                holder.cancel_yuyue.setVisibility(View.VISIBLE);// 取消订单
            } else if (list.get(position).getStatus().equals("2")) {
                holder.yuyue_state.setText("已确认");

                if (list.get(position).getIs_update_order().equals("1")) {
                    holder.yuyue_state.setText("服务商已评估价格");
                    holder.go_pay.setVisibility(View.VISIBLE);// 付款
                } else {
                    holder.yuyue_state.setText("服务商已确认订单");
                    holder.go_pay.setVisibility(View.GONE);// 付款
                    holder.yuyue_buttonll.setVisibility(View.GONE);
                }
            } else if (list.get(position).getStatus().equals("3")) {
                holder.yuyue_state.setText("已支付");
                if (list.get(position).getIs_confirm().equals("1")) {
                    holder.confirm_complete.setVisibility(View.VISIBLE);
                } else {
                    holder.yuyue_buttonll.setVisibility(View.GONE);
                }
            } else if (list.get(position).getStatus().equals("4")) {
                holder.yuyue_state.setText("已完成");
                if (list.get(position).getIs_evaluation().equals("1")) {
                    holder.go_eva.setVisibility(View.VISIBLE);// 去评价
                } else {
                    holder.yuyue_buttonll.setVisibility(View.GONE);
                }
            } else if (list.get(position).getStatus().equals("5")) {
                holder.yuyue_state.setText("已评价");
                holder.detele_yuyue.setVisibility(View.VISIBLE);
                holder.yuyue_buttonll.setVisibility(View.VISIBLE);
            } else if (list.get(position).getStatus().equals("6")) {
                holder.yuyue_state.setText("已取消");
                holder.detele_yuyue.setVisibility(View.VISIBLE);
            }
        }

        holder.order_no.setText("预约编号：" + list.get(position).getOrder_no());
        ImageLoader.getInstance().displayImage(list.get(position).getTitile_img(), holder.order_img);
        holder.order_title.setText(list.get(position).getTitle());
        holder.yuyue_content.setText("分类：" + list.get(position).getType());
        holder.yuyue_money.setText("¥ " + list.get(position).getMoney());

        //        MyYuyueItemAdapter adapter2 = new MyYuyueItemAdapter(context);
        //        holder.scrollview_listview.setAdapter(adapter2);
        //        holder.scrollview_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        //            @Override
        //            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        //                MyYuyueAdapter.this.backCall.deal(R.id.scrollview_listview, position);
        //            }
        //        });
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.yuyue_itemll)
        LinearLayout yuyue_itemll;
        @ViewInject(R.id.item_ll)
        LinearLayout item_ll;
        @ViewInject(R.id.yuyue_buttonll)
        LinearLayout yuyue_buttonll;
        // @ViewInject(R.id.scrollview_listview)
        // MyListView scrollview_listview;
        @ViewInject(R.id.yuyue_state)
        TextView     yuyue_state;
        @ViewInject(R.id.cancel_yuyue)
        TextView     cancel_yuyue;
        @ViewInject(R.id.go_pay)
        TextView     go_pay;
        @ViewInject(R.id.confirm_complete)
        TextView     confirm_complete;
        @ViewInject(R.id.go_eva)
        TextView     go_eva;
        @ViewInject(R.id.detele_yuyue)
        TextView     detele_yuyue;
        @ViewInject(R.id.order_img)
        ImageView    order_img;
        @ViewInject(R.id.order_no)
        TextView     order_no;
        @ViewInject(R.id.order_title)
        TextView     order_title;
        @ViewInject(R.id.yuyue_content)
        TextView     yuyue_content;
        @ViewInject(R.id.yuyue_money)
        TextView     yuyue_money;
        @ViewInject(R.id.chenge_yuyue)
        TextView     chenge_yuyue;
    }
}
